package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        if(null==id){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        int count = wmNewsService.count(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_ADMIND,"无法删除频道，该频道已在其他文章中被引用");
        }
        return removeById(id)?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    /**
     * 分页查询频道
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listChannel(ChannelDto dto) {
        dto.checkParam();
        LambdaQueryWrapper<WmChannel> query = Wrappers.<WmChannel>lambdaQuery();
        if(StringUtils.isNotBlank(dto.getName())){
            query.like(WmChannel::getName,dto.getName());
        }
        Page<WmChannel> page = page(new Page<>(dto.getPage(), dto.getSize()), query);
        return ResponseResult.okResult(page.getRecords());
    }

    /**
     * 新增保存频道信息
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel dto) {
        if(StringUtils.isNotBlank(dto.getName())||StringUtils.isNotBlank(dto.getDescription())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return save(dto)?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 更新频道信息
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel dto) {
        if(dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return updateById(dto) ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
}